// pages/01homePage/05bluetooth/print.js
Page({
    data: {
        status: "",
        sousuo: "",
        connectedDeviceId: "", //已连接设备uuid  
        services: "", // 连接设备的服务  
        characteristics: "",   // 连接设备的状态值  
        writeServicweId: "", // 可写服务uuid  
        writeCharacteristicsId: "",//可写特征值uuid  
        readServicweId: "", // 可读服务uuid  
        readCharacteristicsId: "",//可读特征值uuid  
        notifyServicweId: "", //通知服务UUid  
        notifyCharacteristicsId: "", //通知特征值UUID  
        inputValue: "",
        characteristics1: "", // 连接设备的状态值  
    },

    onLoad: function (opt) {
        //this.getBLEDeviceServices(opt.deviceId)
        this.lanya6(opt.deviceId)
    },

    getBLEDeviceServices: function (deviceId) {
        console.log(deviceId)
        console.log('deviceId')
        var that = this;
        wx.getBLEDeviceServices({
            // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  
            deviceId: deviceId,
            success: function (res) {
                that.setData({
                    services: res.services,
                })
                that.getBLEDeviceCharacteristics(deviceId, res.services)
                console.log(that.data)
                console.log('6获取连接设备的service服务成功this.data')
            },
            fail: function (res) {
                wx.showToast({
                    title: '获取service失败',
                })
            },
        })
    },

    getBLEDeviceCharacteristics: function (deviceId, serviceArr) {
        console.log(deviceId)
        console.log(serviceArr)
        console.log('deviceId + serviceArr')
        var that = this
        if (serviceArr.length < 2) return
        for (var i = 0; i < 2; i++) {
            wx.getBLEDeviceCharacteristics({
                deviceId: deviceId,
                serviceId: serviceArr[i],
                success: function (res) {
                    that.forHandleCharacteristics(serviceArr[i], res.characteristics)
                    console.log(that.data)
                    console.log('that.data')
                },
                fail: function (res) {
                    wx.showToast({
                        title: 'getBLEChar失败',
                    })
                },
            })
        }
    },

    forHandleCharacteristics: function (service, charArr) {
        for (var i = 0; i < charArr.length; i++) {
            if (charArr[i].properties.notify) {
                that.setData({
                    notifyServicweId: service.uuid,
                    notifyCharacteristicsId: charArr[i].uuid,
                })
            }
            if (charArr[i].properties.write) {
                that.setData({
                    writeServicweId: service.uuid,
                    writeCharacteristicsId: charArr[i].uuid,
                })
            } else if (charArr[i].properties.read) {
                that.setData({
                    readServicweId: service.uuid,
                    readCharacteristicsId: charArr[i].uuid,
                })
            }
        }
    },
    /**
	 * 发送 数据到设备中
	 */
    sendCommand: function (e) {
        var hex = e.detail.value.order
        var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
            return parseInt(h, 16)
        }))

        var buffer1 = typedArray.buffer
        this.setData({
            order: hex + ' -> ' + JSON.stringify(buffer1)
        })
        this.writeBLECharacteristicValue(buffer1)
    },

    writeBLECharacteristicValue: function (buffer) {
        var that = this
        wx.writeBLECharacteristicValue({
            deviceId: that.data.connectedDeviceId,
            serviceId: that.data.writeServicweId,
            characteristicId: that.data.writeCharacteristicsId,
            value: buffer,
            success: function (res) {
                var str = "指令发送成功：" + JSON.stringify(res)
                that.setData({
                    result2: str
                })
            },
            fail: function (res) {
                wx.showToast({
                    title: '指令发送失败',
                })
            },
        })
    },

    //************************************************** */
    // 获取连接设备的service服务  
    lanya6: function (deviceId) {
        var that = this;
        wx.getBLEDeviceServices({
            // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  
            deviceId: deviceId,
            success: function (res) {
                console.log('device services:', JSON.stringify(res.services));
                that.setData({
                    services: res.services,
                    msg: JSON.stringify(res.services),
                })

                console.log(that.data)
                console.log('6获取连接设备的service服务成功this.data')
                that.lanya7(deviceId)
            },
            fail: function (res) {
                wx.showToast({
                    title: 'service服务获取失败',
                })
            },
        })
    },
    //获取连接设备的所有特征值  for循环获取不到值  
    lanya7: function (deviceId) {
        var that = this;
        wx.getBLEDeviceCharacteristics({
            // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  
            deviceId: deviceId,//that.data.connectedDeviceId,
            // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取  
            serviceId: that.data.services[0].uuid,
            success: function (res) {
                console.log(res);
                console.log("7获取连接设备的所有特征值res");
                for (var i = 0; i < res.characteristics.length; i++) {
                    if (res.characteristics[i].properties.notify) {
                        console.log("11111111", that.data.services[0].uuid);
                        console.log("22222222222222222", res.characteristics[i].uuid);
                        that.setData({
                            notifyServicweId: that.data.services[0].uuid,
                            notifyCharacteristicsId: res.characteristics[i].uuid,
                        })
                    }
                    if (res.characteristics[i].properties.write) {
                        that.setData({
                            writeServicweId: that.data.services[0].uuid,
                            writeCharacteristicsId: res.characteristics[i].uuid,
                        })

                    } else if (res.characteristics[i].properties.read) {
                        that.setData({
                            readServicweId: that.data.services[0].uuid,
                            readCharacteristicsId: res.characteristics[i].uuid,
                        })

                    }
                }
                console.log('device getBLEDeviceCharacteristics:', res.characteristics);

                that.setData({
                    msg: JSON.stringify(res.characteristics),
                })

                console.log(that.data)
                console.log('7.1获取连接设备的所有特征值 that.data')
            },
            fail: function () {
                console.log("7.1获取连接设备的所有特征值fail");
            },
            complete: function () {
                console.log("complete");
            }
        })

        wx.getBLEDeviceCharacteristics({
            // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  
            deviceId: deviceId,//that.data.connectedDeviceId,
            // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取  
            serviceId: that.data.services[1].uuid,
            success: function (res) {
                for (var i = 0; i < res.characteristics.length; i++) {
                    if (res.characteristics[i].properties.notify) {
                        that.setData({
                            notifyServicweId: that.data.services[1].uuid,
                            notifyCharacteristicsId: res.characteristics[i].uuid,
                        })
                    }
                    if (res.characteristics[i].properties.write) {
                        that.setData({
                            writeServicweId: that.data.services[1].uuid,
                            writeCharacteristicsId: res.characteristics[i].uuid,
                        })

                    } else if (res.characteristics[i].properties.read) {
                        that.setData({
                            readServicweId: that.data.services[1].uuid,
                            readCharacteristicsId: res.characteristics[i].uuid,
                        })

                    }
                }
                console.log('device getBLEDeviceCharacteristics1:', res.characteristics);

                that.setData({
                    msg1: JSON.stringify(res.characteristics),
                })

                console.log(that.data)
                console.log('7.2获取连接设备的所有特征值222 that.data')
            },
            fail: function () {
                console.log("fail1");
            },
            complete: function () {
                console.log("complete1");
            }
        })
    },


    
    
})